﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Workflow.ComponentModel.Design;
using System.Drawing;

namespace OpenComposite.Repository.Tracking
{
	internal sealed class TrackingMenuCommandService : MenuCommandService
	{
		public TrackingMenuCommandService(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{

		}

		public override void ShowContextMenu(CommandID menuID, int x, int y)
		{
			ContextMenuStrip contextMenu = null;
			if ( menuID == WorkflowMenuCommands.PageLayoutMenu ) {
				contextMenu = new ContextMenuStrip();
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.DefaultPage, "Default View", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.PrintPreviewPage, "Print Preview", null);
			} else if ( menuID == WorkflowMenuCommands.ZoomMenu ) {
				contextMenu = new ContextMenuStrip();
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom400Mode, "400%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom300Mode, "300%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom200Mode, "200%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom150Mode, "150%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom100Mode, "100%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom75Mode, "75%", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Zoom50Mode, "50%", null);
				contextMenu.Items.Add(new ToolStripSeparator());
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.ShowAll, "Show All", null);
			} else if ( menuID == WorkflowMenuCommands.PanMenu ) {
				contextMenu = new ContextMenuStrip();
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.ZoomIn, "Zoom In", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.ZoomOut, "Zoom Out", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.Pan, "Navigation Tool", null);
				addWorkflowMenuCommand(contextMenu, WorkflowMenuCommands.DefaultFilter, "Default", null);
			}
			if ( contextMenu != null && contextMenu.Items.Count > 0 ) {
				WorkflowView workflowView = GetService(typeof(WorkflowView)) as WorkflowView;
				if ( workflowView != null )
					contextMenu.Show(workflowView, workflowView.PointToClient(new Point(x, y)));
			}
			base.ShowContextMenu(menuID, x, y);
		}
		private void addWorkflowMenuCommand(ContextMenuStrip context, CommandID commandID,
			string text, Image icon)
		{
			MenuCommand com = this.FindCommand(commandID);
			ToolStripMenuItem item = new ToolStripMenuItem(text, icon, new EventHandler(OnMenuClicked));
			item.Enabled = com.Enabled;
			item.Visible = com.Visible;
			item.Checked = com.Checked;
			item.Tag = com;
			context.Items.Add(item);
		}
		private void OnMenuClicked(object sender, EventArgs e)
		{
			ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
			if ( menuItem != null && menuItem.Tag is MenuCommand ) {
				MenuCommand command = menuItem.Tag as MenuCommand;
				command.Invoke();
			}
		}
	}
}
